from numpy import *
from numpy.random import *
import pygame, os
from pygame.locals import *
from pygame.color import *

N = 50
CELLSIZE = 10
SPELLSIZE = 8
NUMSPELLS = 6
size = (N, N)
work = zeros(size)
neighbours = zeros(size)

def update(world):
        global work, summed
        # neighbour counting
        neighbours[:] = 0
        for x in range(-1, 2):
                for y in range(-1, 2):
                        if x == 0 and y == 0:
                                continue
                        neighbours[max(0,-x):min(-x+N,N), max(0,-y):min(-y+N,N)] += \
                                world[max(0,x):min(x+N,N), max(0,y):min(y+N,N)] >= 32
        # value condition
        neighbours[world != 0] = 0
        # decay
        world -= 2
        #~ world *= 0.6
        # limits        
        world[world >= 32] = 32
        world[world < 1] = 0        
        # rules
        world[neighbours == 2] = 32
        world[neighbours == 4] = 32
        world[neighbours == 6] = 32
        #~ world[neighbours == 6] = 32

#~ print world
def main():
        os.environ['SDL_VIDEO_CENTERED'] = '1'
        pygame.init()
        background = pygame.display.set_mode((800,600))
        #~ background = pygame.display.set_mode(DisplaySize, FULLSCREEN|HWSURFACE|DOUBLEBUF)	
        pygame.display.set_caption('sorc')
        pygame.key.set_repeat(500, 30)
        clock = pygame.time.Clock()
        FPS = 30
        world = zeros(size, dtype="float32")
        for i in range(100):
                world[randint(0,N-1),randint(0,N-1)] = 32
        spellblock = SPELLSIZE * CELLSIZE
        decay =True
        mousemove = False
        spellbook = []
        while 1:
                #handle input events
                for event in pygame.event.get():			
                        if event.type == QUIT:
                                return                        
                        elif event.type == MOUSEBUTTONDOWN:
                                x, y = pygame.mouse.get_pos() 
                                if x in range(680, 680+spellblock):                                        
                                        for i, spell in enumerate(spellbook):                                                
                                                sy = i*(SPELLSIZE+1)*CELLSIZE + 10                                                
                                                if y >= sy and y <  sy + spellblock:
                                                        world[N/2:N/2+SPELLSIZE, N/2:N/2+SPELLSIZE] = spell
                                elif event.button == 1:
                                        mousemove = True
                        elif event.type == MOUSEBUTTONUP:
                                mousemove = False
                        elif event.type == KEYDOWN:
                                if event.key == K_ESCAPE:
                                        return
                                elif event.key == ord(' '):
                                        decay = not decay
                                elif event.key == ord('r'):
                                        #~ for i in range(100):
                                                #~ world[randint(0,N-1),randint(0,N-1)] = 32
                                        #~ spellsize = (SPELLSIZE, SPELLSIZE)
                                        #~ spell = zeros(spellsize)
                                        spell = rand(SPELLSIZE, SPELLSIZE)
                                        threshold = 0.1
                                        spell[spell >= threshold] = 32
                                        spell[spell < threshold] = 0
                                        spell[2:6,2:6] = 0
                                        world[N/2:N/2+SPELLSIZE, N/2:N/2+SPELLSIZE] = spell
                                        if len(spellbook) < NUMSPELLS:
                                                spellbook.append(spell)
                                        else:
                                                spellbook.pop(0)
                                                spellbook.append(spell)
                                                
                                        
                                        #~ for i in range(9):
                                                #~ world[randint(20,27),randint(20,27)] = 32
                                        # draw spellbook
                                        for i, spell in enumerate(spellbook):
                                                pygame.draw.rect(background,  (80,80,80), Rect((680-1,10+i*(spellblock+CELLSIZE)-1), (spellblock+2, spellblock+2)), 1)
                                                for x in range(SPELLSIZE):
                                                        for y in range(SPELLSIZE):
                                                                #~ pygame.draw.rect(background,  (int(world[x, y])*6,0,0), Rect((x*CELLSIZE, y*CELLSIZE),(CELLSIZE, CELLSIZE)), 0)                              
                                                                pygame.draw.rect(background,  (int(spell[x, y])*6,0,0),  \
                                                                Rect((x*CELLSIZE+680, i*(SPELLSIZE+1)*CELLSIZE + y*CELLSIZE+ 10), (CELLSIZE, CELLSIZE)),0)
                                                                #~ Rect((x*CELLSIZE, i*SPELLSIZE*7 + 50 + y*CELLSIZE), (CELLSIZE, CELLSIZE)), 0)
                if mousemove:
                        x, y = pygame.mouse.get_pos() 
                        x /= CELLSIZE
                        y /= CELLSIZE
                        x = min(x, N-1)
                        y = min(y, N-1)
                        world[x, y] = 32                
                #~ background.fill((50,50,50))
                #~ pygame.draw.rect(background, Color('black'), Rect((0,0), (CELLSIZE*N, CELLSIZE*N)), 0)         
                pygame.draw.rect(background, (40,50,40), Rect((0,0), (CELLSIZE*N, CELLSIZE*N)), 0)         
                # draw world
                for x in range(N):
                        for y in range(N):
                                if int(world[x, y]) > 0:
                                        pygame.draw.rect(background,  (int(world[x, y])*6,0,0), Rect((x*CELLSIZE, y*CELLSIZE),(CELLSIZE, CELLSIZE)), 0)
                # draw sorc
                pygame.draw.circle(background,  (0,0,255), ((N/2+SPELLSIZE/2)*CELLSIZE, (N/2+SPELLSIZE/2)*CELLSIZE), CELLSIZE,1)
        #~ pygame.draw.rect(background, Color('gray'), Rect(sim.coord((-1, -1)), (DisplaySize[1], Dis,0playSize[1])), 1)                
                pygame.display.flip()
                if decay:
                        update(world)	
                clock.tick(FPS)		
                
                
main()